<?php

class fvPager implements ArrayAccess, Iterator{
    private $_manager;
    private $_pageCount;
    private $_objects;
    private $_currentPage;
    private $_paramName;
    
    public function __construct($manager, $objects = null, $paramName = "page") {
        $this->_manager = $manager;
        $this->_objects = $objects;
        $this->_paramName = $paramName;
    }
    
    public function paginate($where = null, $order = null, $params = array()) {
        
     	if ($objPerPage = fvSite::$fvSession->get("pp")) {
            $objPerPage = fvSite::$fvSession->get("pp");
        }elseif ( $objPerPage = fvSite::$fvConfig->get("modules." . fvRoute::getInstance()->getModuleName() . ".pager.show_per_page")) {
        	$objPerPage = fvSite::$fvConfig->get("modules." . fvRoute::getInstance()->getModuleName() . ".pager.show_per_page");	
        }elseif ($objPerPage = fvSite::$fvConfig->get("pager.show_per_page")){
        	$objPerPage = fvSite::$fvConfig->get("pager.show_per_page");
        }
        else{
        	$objPerPage = 10;
        }
        
        $this->_currentPage = fvRequest::getInstance()->getRequestParameter($this->_paramName, 'int');
        
        if (!is_null($this->_objects)) {
            $count = count($this->_objects);
        } else {
            $count = $this->_manager->getCount($where, $params);
        }
        $this->_pageCount = ceil($count / $objPerPage);
        
        if (!is_null($this->_objects)) {
            $this->_objects = array_slice($this->_objects, $this->_currentPage * $objPerPage, $objPerPage);
        } else {
            $this->_objects = $this->_manager->getAll($where, $order, 
                ((int)($this->_currentPage * $objPerPage)) . "," . $objPerPage, $params);
        }
        
        return $this;
    }
    
    public function getData() {
        return $this->_objects;
    }
    
    public function hasPaginate() {
        return ($this->_pageCount > 1);
    }
    
    protected function _checkPage($pageNum) {
        if ($this->_pageCount < 10) return true;
        if (($pageNum < 3) || (($this->_pageCount - $pageNum) < 4)) return true;
        if (abs($pageNum - $this->_currentPage) < 3) return true;
        
        return false;
    }
    
    public function showPager ($full = false) {
        $content = "";
        $separator = false;
        for ($i = 0; $i < $this->_pageCount; $i++) {
            if ($full || $this->_checkPage($i)) {
                $separator = false;
                $href = fvRequest::getInstance()->parseQueryString($_SERVER['REQUEST_URI'], $this->_paramName, $i);
                if ($i == $this->_currentPage)
                    $content .= "<span>" . ($i + 1) . "</span>";
                else $content .= "<a href='$href'>" . ($i + 1) . "</a>";
            } else {
                if (!$separator) {
                   $content .= "<span class='sep'>...</span>"; 
                   $separator = true;
                }
            }
        }
        
        return $content;
    }
    
	public function showUserPager ($full = false) {
        $content = "";
		if (0 <= $this->_currentPage-1){
        	$href = fvRequest::getInstance()->parseQueryString($_SERVER['REQUEST_URI'], $this->_paramName, $this->_currentPage-1);
        	$content .= "<a href='{$href}' title=''>попередня</a>";
        }
        else{
        	$content .= "<span>попередня</span>";
        }
        $separator = false;
        for ($i = 0; $i < $this->_pageCount; $i++) {
            if ($full || $this->_checkPage($i)) {
                $separator = false;
                $href = fvRequest::getInstance()->parseQueryString($_SERVER['REQUEST_URI'], $this->_paramName, $i);
                if ($i == $this->_currentPage)
                    $content .= "<span><strong>" . ($i + 1) . "</strong></span>";
                else 
                	$content .= "<span><a href='$href'>" . ($i + 1) . "</a></span>";
            } else {
                if (!$separator) {
                   $content .= "<span class='sep'>...</span>"; 
                   $separator = true;
                }
            }
             
        }
        if ($this->_pageCount > $this->_currentPage+1){
        	$href = fvRequest::getInstance()->parseQueryString($_SERVER['REQUEST_URI'], $this->_paramName, $this->_currentPage+1);
        	$content .= "<a href='{$href}' title=''>наступна</a>";
        }
        else{
        	$content .= "<span>наступна</span>";
        }
        return $content;
    }
    
    public function getCurrentPage() {
        return $this->_currentPage;
    }
    
    public function getPageCount() {
        return $this->_pageCount;
    }
    
    function offsetExists($offset) {
        return isset($this->_objects[$offset]);
    }

    function offsetGet($offset) {
        return $this->_objects[$offset];
    }

    function offsetUnset($offset) {
        unset($this->_objects[$offset]);
    }
    
    function offsetSet($offset, $newValue) {
        $this->_objects[$offset] = $newValue;
    }
    
    public function rewind() {
        reset($this->_objects);
    }

    public function current() {
        $var = current($this->_objects);
        return $var;
    }
    
    public function key() {
        $var = key($this->_objects);
        return $var;
    }
    
    public function next() {
        $var = next($this->_objects);
        return $var;
    }
    
    public function valid() {
        $var = $this->current() !== false;
        return $var;
    }

    public function getObjects() {
        return $this->_objects;
    }
}
